package com.yiqi.news.mvp.presenter

import android.app.Application

import com.jess.arms.integration.AppManager
import com.jess.arms.di.scope.ActivityScope
import com.jess.arms.mvp.BasePresenter
import com.jess.arms.http.imageloader.ImageLoader
import com.yiqi.huize.isNull
import com.yiqi.news.app.utils.RxUtils
import com.yiqi.news.entity.BaseResponse
import com.yiqi.news.entity.NewsDetail
import me.jessyan.rxerrorhandler.core.RxErrorHandler
import javax.inject.Inject

import com.yiqi.news.mvp.contract.VideoDetailContract
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 06/19/2019 18:16
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
class VideoDetailPresenter
@Inject
constructor(model: VideoDetailContract.Model, rootView: VideoDetailContract.View) :
        BasePresenter<VideoDetailContract.Model, VideoDetailContract.View>(model, rootView) {
    @Inject
    lateinit var mErrorHandler: RxErrorHandler
    @Inject
    lateinit var mApplication: Application
    @Inject
    lateinit var mImageLoader: ImageLoader
    @Inject
    lateinit var mAppManager: AppManager


    override fun onDestroy() {
        super.onDestroy();
    }

    fun getNewsData(newsId: String) {
        if (newsId.isNullOrEmpty()) return
        mModel.getNewsDetail(newsId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtils.applySchedulers(mRootView))
                .subscribe(object : ErrorHandleSubscriber<BaseResponse<NewsDetail>>(mErrorHandler) {
                    override fun onNext(t: BaseResponse<NewsDetail>) {

                        if (t.isSuccess && !t.data.isNull()) {
                            mRootView.showNewsDetail(t.data)

                        } else {
                            mRootView.showNewsError(t.msg)
                        }

                    }

                    override fun onError(t: Throwable) {
                        super.onError(t)
                    }
                })
    }


    fun doCommentNews(materialId: String, materialType: Int, commentInfo: String) {
        if (commentInfo.isNullOrEmpty()) {
            mRootView.showError("请输入评论类容")
            return
        }
        var map = HashMap<String, Any?>()
        map["materialId"] = materialId
        map["materialType"] = materialType
        map["commentInfo"] = commentInfo
        mModel.toComment(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtils.applySchedulers(mRootView))
                .subscribe(object : ErrorHandleSubscriber<BaseResponse<NewsDetail>>(mErrorHandler) {

                    override fun onNext(t: BaseResponse<NewsDetail>) {
                        if (t.isSuccess){
                            mRootView.showCommentSucess()
                        }else{
                            mRootView.showError(t.msg)
                        }
                    }
                })
    }

    fun doLikeNews(type: Int, typeId: String) {
//        type (integer): 点赞类型 1、作品点赞 2、 评论点赞 ,
//        typeId (integer): 对应的素材作品或评论的id
        var map = HashMap<String, Any?>()
        map["type"] = type
        map["typeId"] = typeId
        mModel.toLike(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtils.applySchedulers(mRootView))
                .subscribe(object : ErrorHandleSubscriber<BaseResponse<NewsDetail>>(mErrorHandler) {

                    override fun onNext(t: BaseResponse<NewsDetail>) {
                        if (t.isSuccess){
                            mRootView.showLikeSucess()
                        }else{
                            mRootView.showError(t.msg)
                        }
                    }
                })
    }

    fun doNewsCollection( materialId: String) {
        var map = HashMap<String, Any?>()
        map["materialId"] = materialId
        mModel.toCollection(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtils.applySchedulers(mRootView))
                .subscribe(object : ErrorHandleSubscriber<BaseResponse<NewsDetail>>(mErrorHandler) {

                    override fun onNext(t: BaseResponse<NewsDetail>) {
                        if (t.isSuccess){
                            mRootView.showCollectionSucess()
                        }else{
                            mRootView.showError(t.msg)
                        }
                    }
                })
    }

}
